package com.aiit.controller;

import com.aiit.pojo.SysUser;
import com.aiit.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/doLogin")
    public String doLogin( SysUser user) {
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername() ,user.getPassword());
        token.setRememberMe(true);
        subject.login(token);

        return "/paper/allPaper";
//        if(subject.hasRole("admin")){
//            return "redirect:/manager/index";
//        }
//
//        if(subject.hasRole("user")){
//            return "redirect:/index";
//        }
//        return "/login/permsError";
    }

    @RequestMapping("/doRegist")
    public String doRegist( SysUser user) {
        sysUserService.addSysUser(user);
        return "login/login";
    }


    @RequestMapping("/loginPage")
    public ModelAndView loginPage() {
        ModelAndView v=new ModelAndView();
        v.setViewName("login/login");
        return v;
    }


    @RequestMapping("/error")
    public String error( ) {
        return "/login/permsError";
    }


    @RequestMapping("/regist")
    public  String  regist() {
        return "/login/regist";
    }
}
